package com.ruoyi.common.utils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

public class ListDataUtil {
    /**
     *
     * @Description //TODO 根据多个参数进行排序
     * @Param [list, name, names]
     *
     **/
    public static List<Map<String, Object>> twoObjectascendingOrder(List<Map<String, Object>> list, String name, String names) {
        // 根据多个字段排序
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> map1, Map<String, Object> map2) {
                // 先按照name字段升序排序
                String name1 = (String) map1.get(name);
                String name2 = (String) map2.get(name);
                int result = name1.compareTo(name2);

                // 如果name相同，则按照age字段降序排序
                if (result == 0) {
                    String age1 = String.valueOf(String.valueOf(map1.get(names)));
                    String age2 = String.valueOf(String.valueOf(map2.get(names)));
                    result = age2.compareTo(age1);
                }

                return result;

            }
        });

        return list;
    }

    public static List<Map<String, Object>> doubleascendingOrder(List<Map<String, Object>> list, String name) {
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Object str1 = o1.get(name);
                Object str2 = o2.get(name);
                //判断如果数据为空或者-赋值最大
                if (str1 != null && !"-".equals(str1) && !"0.0".equals(str1)) {

                } else {
                    str1 = "99999";
                }
                if (str2 != null && !"-".equals(str2) && !"0.0".equals(str2)) {

                } else {
                    str2 = "99999";
                }
                Double aDouble1 = Double.valueOf(String.valueOf(str1));
                Double aDouble2 = Double.valueOf(String.valueOf(str2));
                return aDouble1.compareTo(aDouble2);
            }
        });
        return list;
    }

}
